#include "statement.h"

static STAILQ_HEAD(unresolved, kw_statement) unresolved_list = STAILQ_HEAD_INITIALIZER(unresolved_list);
static STAILQ_HEAD(resolved, kw_statement) resolved_list = STAILQ_HEAD_INITIALIZER(resolved_list);

void kw_add_statement(struct kw_statement *statement)
{
  if (cmd_in_debug())
    printf("add statement %s\n", statement->templ);
  STAILQ_INSERT_TAIL(&unresolved_list, statement, entry);
}

struct kw_statement *statement_pop(void)
{
  if (!STAILQ_EMPTY(&unresolved_list)) {
    struct kw_statement *e = STAILQ_FIRST(&unresolved_list);
    STAILQ_REMOVE_HEAD(&unresolved_list, entry);
    STAILQ_INSERT_TAIL(&resolved_list, e, entry);
    return e;
  }
  return NULL;
}

BOLT(show_statement, "cmd statement")
{
  struct kw_statement *lookup;
  printf ("unresolved list:\n");
  STAILQ_FOREACH(lookup, &unresolved_list, entry) {
    printf ("  %s - %p [%s]\n", lookup->templ, lookup->func, lookup->name);
  }
  
  printf ("resolved list:\n");
  STAILQ_FOREACH(lookup, &resolved_list, entry) {
    printf ("  %s - %p [%s]\n", lookup->templ, lookup->func, lookup->name);
  }
}


